package com.zhl.service.impl;

import com.zhl.dao.MeetingDao;
import com.zhl.dao.MeetingJoinDao;
import com.zhl.model.Meeting;
import com.zhl.model.MeetingJoin;
import com.zhl.service.MeetingService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class MeetingServiceImpl implements MeetingService {

    MeetingDao meetingDao =  new MeetingDao();
    MeetingJoinDao meetingJoinDao = new MeetingJoinDao();

    @Override
    public List<Meeting> findMeetings(String title,String status) {
        Integer s = null;
        if (status != null){
            if (status.equals("未开始")){
                s = 0;
            }else if (status.equals("进行中")){
                s = 1;
            }else if (status.equals("已结束")){
                s = 2;
            }else {
                s = null;
            }
        }
        return meetingDao.findAllMeeting(title,s);
    }

    @Override
    public Map<String, Object> findMeetingById(Integer id) {
        Map<String, Object> map =  meetingDao.findMeetingById(id);
        String makeUser = (String) map.get("make_user");
        String[] users = makeUser.split(",");
        map.put("userSize",users.length);
        return map;
    }

    @Override
    public Integer updateJoinById(Integer u_Id, Integer m_Id) {
        Integer a = 1;
        Meeting joinById = meetingDao.findJoinById(u_Id, m_Id);
        if (joinById == null){
            String userBymId = meetingDao.findUserBymId(m_Id);
            String[] userIds = userBymId.split(",");
            List<String> list = new ArrayList<>(Arrays.asList(userIds));
            if (list.contains(String.valueOf(u_Id))){
                MeetingJoin meetingJoin = new MeetingJoin();
                meetingJoin.setUId(u_Id);
                meetingJoin.setCId(m_Id);
                meetingJoinDao.Save(meetingJoin);
                a = 0;
            }else {
                a = 2;
            }
        }
        return a;
    }

    @Override
    public boolean findJoinById(Integer uId, Integer mId) {
        Meeting joinById = meetingDao.findJoinById(uId, mId);
        if (joinById == null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean saveMeeting(Meeting meeting) {
        return meetingDao.Save(meeting);
    }
}
